package com.example.lanou.mynews.single;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.lanou.mynews.application.MyApplication;
import com.example.lanou.mynews.db.dao.DaoMaster;
import com.example.lanou.mynews.db.dao.DaoSession;
import com.example.lanou.mynews.db.dao.Titles;
import com.example.lanou.mynews.db.dao.TitlesDao;
import com.example.lanou.mynews.face.DbName;

import java.util.List;

import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/1/23.
 */
public class SingleDao implements DbName {


    /**
     * Created by Risky on 15/10/29.
     */

    private static final String DATABASE_NAME = "Titles.db";

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private TitlesDao titlesDao;
    private SingleDao singleDao;

    private SingleDao() {
        context = MyApplication.getContext();
    }

    private SingleDao getDao() {
        if (null == singleDao) {
            synchronized (SingleDao.class) {
                if (null == singleDao)
                    singleDao = new SingleDao();
            }
        }
        return singleDao;
    }

    private DaoMaster.DevOpenHelper getHelper() {
        if (null == helper) {
            synchronized (SingleDao.class) {
                if (null == helper) {
                    helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
                }
            }
        }
        return helper;
    }

    private SQLiteDatabase getDb() {
        if (null == db) {
            synchronized (SingleDao.class) {
                if (null == db) {
                    db = getHelper().getWritableDatabase();
                }
            }
        }
        return db;
    }

    private DaoMaster getDaoMaster() {
        if (null == daoMaster) {
            synchronized (SingleDao.class) {
                if (null == daoMaster) {
                    daoMaster = new DaoMaster(getDb());
                }
            }
        }
        return daoMaster;
    }

    public DaoSession getSesson() {
        if (null == daoSession) {
            synchronized (SingleDao.class) {
                if (null == daoSession) {
                    daoSession = getDaoMaster().newSession();
                }
            }
        }
        return daoSession;
    }

    public static SingleDao getInstance() {
        return new SingleDao();
    }

    /***
     * you need to use the method of getDao first
     *
     * @param titles
     */
    public void addTitle(Titles titles) {
        try{
        getSesson().getTitlesDao().insert(titles);
        }catch (Exception e) {
            Log.d("SingleDao", "已经存在主键");
        }
    }

    /***
     * you need to use the method of getDao first
     */
    public List<Titles> getAllTitles() {
        List<Titles> data =getSesson().getTitlesDao().loadAll();
        db.close();
        return data;
    }

    public List<Titles> getNotLike() {
        List<Titles> data=getSesson().getTitlesDao().queryBuilder().where(TitlesDao.Properties.Like.eq(2)).build().list();
        db.close();
        return data;
    }

    public List<Titles> getLikeTitle() {
        List<Titles> data = getSesson().getTitlesDao().queryBuilder().where(TitlesDao.Properties.Like.eq(1)).build().list();
        db.close();
        return data;
    }

    public void upDataTitle(Titles titles) {
        getSesson().getTitlesDao().update(titles);
        db.close();
    }

    public void upDataTitle(String title, int like) {
        Query query =getSesson().getTitlesDao().queryBuilder().where(TitlesDao.Properties.Name.eq(title)).build();
        Titles titles = (Titles) query.list().get(0);
        titles.setLike(like);
        upDataTitle(titles);
        db.close();
    }

    /***
     * get a Title object by a title name;
     * @param title
     * @return
     */
    public  Titles getTitle(String title){
        Titles titles =getSesson().getTitlesDao().queryBuilder().where(TitlesDao.Properties.Name.eq(title)).build().list().get(0);
        db.close();
        return titles;
    }


}


